package com.jackrain.nea.fc.vp.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

/**
 * 会员中心购物券服务接口mapper层
 *
 * @author wuwenxue
 * @date 2017/11/13
 **/
@Mapper
public interface ViewCvouMapper {
    /**
     * 外键关联用户表USERS
     *
     * @param ecode 固定传输用户编号：INTER99999
     * @return Long
     */
    @Select("select ID from USERS where NAME=#{ecode}")
    Long userID(String ecode);

    /**
     * 将数据插入购物券表[VP_C_VOU]中
     *
     * @param string sql
     * @return int
     */
    @InsertProvider(type = ViewCvouSQL.class, method = "insertToVou")
    int insertAll(String string);

    /**
     * 查看核销的购物券是否存在
     * @param ecode 券号
     * @return int
     */
    @Select("select count(1) from VP_C_VOU where ECODE=#{ecode}")
    int isVou(String ecode);

    /**
     * POS消费核销之后修改为：“Y”
     *
     * @param jsonObject 修改的数据
     * @return int
     */
    @UpdateProvider(type = ViewCvouSQL.class, method = "updateToVou")
    int updateToVerifyed(JSONObject jsonObject);

    class ViewCvouSQL {
        public String insertToVou(String string) {
            return string;
        }

        public String updateToVou(JSONObject jsonObject) {
            String sql = new SQL() {
                {
                    UPDATE("VP_C_VOU");
                    for (String key : jsonObject.keySet()) {
                        if (!"ECODE".equals(key)) {
                            SET(key + "=#{" + key + "}");
                        }
                    }
                    WHERE("ECODE=#{ECODE}");
                }
            }.toString();
            return sql;
        }
    }


}
